package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.util.listener.OnTextChangeListener;

/**
 * Created by jack on 2017/11/4
 */

public class TextWithInputUIComponent extends LinearLayout {
    public TextWithInputUIComponent(Context context) {
        super(context);
    }

    public TextWithInputUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TextWithInputUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvName;
    private EditText etInput;
    private TextView tvRight;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvName = (TextView) findViewById(R.id.ltwi_tv_name);
        etInput = (EditText) findViewById(R.id.ltwi_et_input);
        tvRight = (TextView) findViewById(R.id.ltwi_tv_right);
    }

    public void setName(String name) {
        tvName.setText(FRString.valueOf(name));
    }

    public void setRightName(String content) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(content);
    }

    public void setVerifyStyle() {
        tvRight.setBackgroundResource(R.drawable.mainbg_2radius);
        tvRight.setTextSize(11);
    }

    public void setRightEnable(boolean isEnable) {
        tvRight.setEnabled(isEnable);
    }

    public void setInputEnable(boolean isEnable) {
        etInput.setEnabled(isEnable);
    }

    public void setInputMaxLength(int maxLength) {
        etInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }

    public void setInputHint(String hint) {
        SpannableString ss = new SpannableString(FRString.valueOf(hint));
        ss.setSpan(new AbsoluteSizeSpan(13, true), 0, hint.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        etInput.setHint(ss);
    }

    public String getInputContent() {
        return etInput.getText().toString().trim();
    }

    public void setInputContent(String content) {
        etInput.setText(FRString.valueOf(content));
    }

    public void setOnRightClickListener(OnClickListener onRightClickListener) {
        tvRight.setOnClickListener(onRightClickListener);
    }

    public void setOnInputListener(OnTextChangeListener onInputListener) {
        etInput.addTextChangedListener(onInputListener);
    }
}